package com.ruoyi.htzj.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.htzj.domain.HtzjScanRecords;
import com.ruoyi.htzj.service.HtzjScanRecordsService;
import com.ruoyi.htzj.mapper.HtzjScanRecordsMapper;
import org.springframework.stereotype.Service;

/**
 * @author xq
 * @description 针对表【htzj_scan_records(扫码记录表，存储用户的扫码记录)】的数据库操作Service实现
 * @createDate 2025-03-24 03:09:17
 */
@Service
public class HtzjScanRecordsServiceImpl extends ServiceImpl<HtzjScanRecordsMapper, HtzjScanRecords>
    implements HtzjScanRecordsService {

    @Override
    public TableDataInfo<HtzjScanRecords> queryPageList(HtzjScanRecords scanRecords, PageQuery pageQuery) {
        // 创建分页对象
        Page<HtzjScanRecords> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        // 创建查询条件包装器
        LambdaQueryWrapper<HtzjScanRecords> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(scanRecords.getStreetId() != null, HtzjScanRecords::getStreetId, scanRecords.getStreetId());
        queryWrapper.orderByDesc(HtzjScanRecords::getScanTime);
        // 执行分页查询
        Page<HtzjScanRecords> resultPage = this.page(page, queryWrapper);
        // 将分页结果封装到 TableDataInfo 中
        return TableDataInfo.build(resultPage);
    }

    @Override
    public Integer countByStreetId(Integer streetId) {
        // 创建查询条件包装器
        LambdaQueryWrapper<HtzjScanRecords> queryWrapper = new LambdaQueryWrapper<>();
        // 添加根据 streetId 查询的条件
        queryWrapper.eq(HtzjScanRecords::getStreetId, streetId);
        // 执行统计查询
        return Math.toIntExact(this.count(queryWrapper));
    }

    @Override
    public R<String> addScanRecord(Integer streetId) {
        if (streetId == null) {
            return R.fail("街道ID不能为空");
        }
        HtzjScanRecords scanRecords = new HtzjScanRecords();
        scanRecords.setStreetId(streetId);
        if (this.save(scanRecords)) {
            return R.ok();
        } else {
            return R.fail("添加失败");
        }
    }
}




